"""
输入：”任意”算数表达式字符串，例如a+b*c-d/e，其中a,b,c,d,e为数值。（不使用eval）
输出：计算结果
例如：
输入：2+3.1*4-8.2/2
输出：10.3
1、仅考虑加减乘除实现（必须）
2、考虑括号实现（加分项，可选）
2+(3.1*4)-(8.2/2)
"""
string = ""  # 初步校验的表达式
sum = 0  # 计算的结果
x = input("输入表达式：")


def func(num):
    if num.isdigit():
        return True
    if num == "+" or num == "-" or num == "*" or num == "/" or num == "(" or num == ")" or num == ".":
        return True
    return False


xx = filter(func, x)
for i in xx:
    string = string + i

try:
    xx = exec("sum={}".format(string))
    print("计算结果为：", round(sum, 3))
except Exception as e:
    print("输入的表达有误！")


"""
获取TB商品信息，输入任意商品ID，返回该商品信息。（不使用Selenium）
商品信息包含以下两部分：
1、商品基本信息
商品标题、主图、月销量、原价、优惠价等
2、优惠券信息

"""

import requests
import time
import json


class TB:
    def __init__(self):
        self.coupon = []
        self.header = {
            "referer": "https://pages.tmall.com/",
            "user-agent": "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Mobile Safari/537.36",
            "cookie": "cna=XcenFwGFcFwCAatzeVWTYooV; hng=CN%7Czh-CN%7CCNY%7C156; xlly_s=1; _l_g_=Ug%3D%3D; login=true; lid=%E9%9C%B8%E6%B0%94%E7%9A%842216934861; t=fcd93188df06f25cb06bdb761be84c28; enc=JF9RKzTCLKahrxkB97eN3z0h6vp1S35jARGRvUY%2ByArDOP9TGjYKhN7wOFVJDFZzecFOVjtsXO%2B6cj0VwNsDHA%3D%3D; _tb_token_=3388d9bbb3670; cookie2=1726e85c58557391459b9cac18bf502b; _m_h5_tk=6d0c7ce22d23b22819c2483b1acc62c8_1604764458286; _m_h5_tk_enc=62a60bab2cbe253d80ef215db38310b0; dnk=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; uc1=cookie15=UtASsssmOIJ0bQ%3D%3D&cookie21=WqG3DMC9Edo1TBf%2BfpfnBg%3D%3D&cookie14=Uoe0abXOmNEtzw%3D%3D&existShop=false; uc3=vt3=F8dCufOHwAt9dTu8hI8%3D&nk2=sE6NkOtYY%2FVDWJ1wymnVTQ%3D%3D&id2=UU6hRiWBUBOyyQ%3D%3D&lg2=URm48syIIVrSKA%3D%3D; tracknick=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; uc4=id4=0%40U2xsBht3XQz4f%2B%2BXp8T%2FLxcDpiND&nk4=0%40srYN9PmlfOJ3FrdT5l9MvK8icEuYStQJAu3P; unb=2602534947; lgc=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; cookie1=B0BWmHHam1iCQGGINrJYO%2B3Fr3xZy1cY66%2BcpgkFOUw%3D; cookie17=UU6hRiWBUBOyyQ%3D%3D; _nk_=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; sgcookie=E100D5S0kd5oQXhyQVav%2FCmPLCrpavFu5xmfoRDD4tPmIXiTm1P%2FSpNaJ7lIYlYNSbTn%2BiMKg0fFTmSD18Z0QFfH9Q%3D%3D; sg=175; csg=6dc03e2d",
        }

    # title img
    def content_html(self, id):
        url = "https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?jsv=2.6.1&appKey=12574478&t={}&sign=f39b1e0244353a01f165b05f1b85df5a&api=mtop.taobao.detail.getdetail&v=6.0&isSec=0&ecode=0&AntiFlood=true&AntiCreep=true&H5Request=true&ttid=2018%40taobao_h5_9.9.9&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22spm%22%3A%22a215s.7406091.recommend.17.35a46770tIUm7l%22%2C%22id%22%3A%22626366582836%22%2C%22scm%22%3A%221007.18975.103645.0%22%2C%22pvid%22%3A%22a258de82-e16f-4874-9418-f89ed55c8d94%22%2C%22utparam%22%3A%22%7B%5C%22ranger_buckets_native%5C%22%3A%5C%22%5C%22%2C%5C%22x_object_type%5C%22%3A%5C%22item%5C%22%2C%5C%22mtx_ab%5C%22%3A8%2C%5C%22mtx_sab%5C%22%3A0%2C%5C%22scm%5C%22%3A%5C%221007.18975.103645.0%5C%22%2C%5C%22x_object_id%5C%22%3A%5C%22626366582836%5C%22%7D%22%2C%22itemNumId%22%3A%22626366582836%22%2C%22itemId%22%3A%22626366582836%22%2C%22exParams%22%3A%22%7B%5C%22spm%5C%22%3A%5C%22a215s.7406091.recommend.17.35a46770tIUm7l%5C%22%2C%5C%22id%5C%22%3A%5C%22626366582836%5C%22%2C%5C%22scm%5C%22%3A%5C%221007.18975.103645.0%5C%22%2C%5C%22pvid%5C%22%3A%5C%22a258de82-e16f-4874-9418-f89ed55c8d94%5C%22%2C%5C%22utparam%5C%22%3A%5C%22%7B%5C%5C%5C%22ranger_buckets_native%5C%5C%5C%22%3A%5C%5C%5C%22%5C%5C%5C%22%2C%5C%5C%5C%22x_object_type%5C%5C%5C%22%3A%5C%5C%5C%22item%5C%5C%5C%22%2C%5C%5C%5C%22mtx_ab%5C%5C%5C%22%3A8%2C%5C%5C%5C%22mtx_sab%5C%5C%5C%22%3A0%2C%5C%5C%5C%22scm%5C%5C%5C%22%3A%5C%5C%5C%221007.18975.103645.0%5C%5C%5C%22%2C%5C%5C%5C%22x_object_id%5C%5C%5C%22%3A%5C%5C%5C%22626366582836%5C%5C%5C%22%7D%5C%22%7D%22%2C%22detail_v%22%3A%228.0.0%22%2C%22utdid%22%3A%221%22%7D".format(
            id)
        response = requests.get(url=url, headers=self.header)
        time.sleep(3)
        data = response.text.strip(" mtopjsonp1(")
        data = data.strip(");")
        json_data = json.loads(data)
        title = json_data['data']["item"]["title"]  # 标题
        print("标题", title)
        img = json_data["data"]["item"]["images"]  # 标题
        print("主图片", img)

    # 月销量、原价、优惠价
    def data_html(self, id):
        parent = {
            "itemId": "{}".format(id),
            "sellerId": "2034387967",
            "modules": "dynStock,qrcode,viewer,price,duty,xmpPromotion,delivery,activity,fqg,zjys,couponActivity,soldQuantity,page,originalPrice,tradeContract"
        }
        header = {
            "referer": "https://item.taobao.com/item.htm?spm=a230r.1.14.70.48111dd8i7mud1&id={}&ns=1&abbucket=7".format(
                id),
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
            "cookie": "cna=XcenFwGFcFwCAatzeVWTYooV; t=8d979e5242099448c9873f4c7a076f0a; cookie2=14a3aa57c44763eb38e380ee07f9d0e0; v=0; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; _samesite_flag_=true; miid=2045236895702146442; xlly_s=1; tk_trace=oTRxOWSBNwn9dPyorMJE%2FoPdY8zfvmw%2Fq5v3h2WGHuet%2BOuHFb9QOtYFO6aQggSX5MefxDqhcPcy6aTa8du%2FI5diFhlWQRT%2FH9OX%2B6I05pUjHpPe%2BHbIoZ04Av9esxOwM8sifJlJz9ghAN1BRX06meAStxrM58qC3ouO2EeDB6KQdWReoW5Jm8KUZTwUhbQNkmnjkT63BzDnAOG39gbn%2FbgubYj6Pq6MS%2By3%2BoebaAkOIpsYo7jtXTRBCHfLWtKe3Da908Pag%2B0eoihA%2F2PsZCjJy%2FF53dA%3D; lLtC1_=1; sgcookie=E100%2Bl5EKkpeb4sNUrs9JoQ2b0fqnrbsQsJjEILc8huaQ%2Bo%2BiPRVBetQh1i1agXuoGmuo60rkRyqtqO2Yz8kElqYsQ%3D%3D; unb=2602534947; uc3=lg2=V32FPkk%2Fw0dUvg%3D%3D&nk2=sE6NkOtYY%2FVDWJ1wymnVTQ%3D%3D&vt3=F8dCufOHxglIvZylRu8%3D&id2=UU6hRiWBUBOyyQ%3D%3D; csg=df96cbe2; lgc=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; cookie17=UU6hRiWBUBOyyQ%3D%3D; dnk=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; skt=556d22da270268c9; existShop=MTYwNDczMDU0Mw%3D%3D; uc4=id4=0%40U2xsBht3XQz4f%2B%2BXp8T%2FLxECyq%2Bp&nk4=0%40srYN9PmlfOJ3FrdT5l9MvK8icEuYStK1Za8n; tracknick=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; _cc_=VFC%2FuZ9ajQ%3D%3D; _l_g_=Ug%3D%3D; sg=175; _nk_=%5Cu6E38%5Cu76CA%5Cu7AE01246268651; cookie1=B0BWmHHam1iCQGGINrJYO%2B3Fr3xZy1cY66%2BcpgkFOUw%3D; mt=ci=71_1; ubn=p; enc=wBeHsWdIRL2WNCGCHI6x3kvJ4dHSIW%2F8Kf7NXOnKABCx2h9wHdoiWnYlr8td6B2TjJtNfN0hN02KRj7%2FNdJMkg%3D%3D; ucn=center; uc1=pas=0&cookie14=Uoe0abXI2UaY9Q%3D%3D&cookie21=Vq8l%2BKCLiv0MzbofZcj7fA%3D%3D&cookie15=U%2BGCWk%2F75gdr5Q%3D%3D&cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&existShop=false; _m_h5_tk=b2cbafa3395bf8de4cc588c8ee69790f_1604747607623; _m_h5_tk_enc=f91777b04b218c1cc6ec0375dceeb319; _tb_token_=eb7a631b7d66e; isg=BK6u80TLfT_Q3YnbNk_tPIZQ_wRwr3KpOJDfuNh3rLFhu08VQD_QuMX6cydXZGrB; l=eBTdACDRO6kBUi7KBO5Zourza77teIRb81PzaNbMiInca15fsFSAwNQV4I7D-dtjgtfAcetyGmkaaRKQv3adVxDDBeDzH13fPxJ6-; tfstk=c_gCBNcKi9XIL1xP8XOaTh1LwmaVZDp_sHwKODHW0WMGuxlCieb4h55nqObTD51..",
        }
        url = "https://detailskip.taobao.com/service/getData/1/p1/item/detail/sib.htm"
        response = requests.get(url, headers=header, params=parent).text
        data = json.loads(response)
        success_sum = data.get("data")["soldQuantity"]["confirmGoodsCount"]
        price = data.get("data")["price"]
        Current_price = data.get("data")["promotion"]["promoData"]["def"][0]["price"]
        print("月销售", success_sum)
        print("原价", price)
        print("现价", Current_price)

    # 消费卷
    def num_html(self, id):
        url = "https://h5api.m.tmall.com/h5/mtop.macao.market.activity.applycoupon.querycouponsfordetail/1.0/?jsv=2.4.16&appKey=12574478&t={}&sign=b3ea9910adc545f1ddf2a3cdd60f85db&api=mtop.macao.market.activity.applycoupon.querycouponsfordetail&v=1.0&timeout=30000&ttid=600000%40taobao_android_7.8.2.50823&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=%7B%22detail_v%22%3A%223.1.2%22%2C%22from%22%3A%22detail%22%2C%22pageType%22%3A%22weex%22%2C%22itemId%22%3A%22522186974070%22%2C%22sellerId%22%3A%22129663914%22%2C%22sellerType%22%3A%22C%22%2C%22ttid%22%3A%22600000%40taobao_android_7.8.2.50823%22%7D".format(
            id)
        coupon = []
        response = requests.get(url, headers=self.header)
        data = response.text.strip(" mtopjsonp1(")
        data = data.strip(");")
        json_data = json.loads(data)
        print(json_data)
        # data_list = json_data.get("data")["coupons"]
        # for i in data_list:
        #     new_i = i['couponList']
        #     for j in new_i:
        #         coupon.append(j["subtitles"][0])
        #
        # print("消费卷", coupon)

    def run(self):
        while True:
            id = input(r"请输入商品的id：")  # 626366582836
            self.content_html(id)
            self.data_html(id)
            self.num_html(id)


if __name__ == '__main__':
    static = TB()
    static.run()
